package cn.gok.homework;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Calendar;

public class Homework {
    public static void createFile(File file) throws IOException {
        File fileParent = file.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdir();
            fileParent.mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    public static void showDir(File file) {
        File[] file1 = file.listFiles();
        for (File files : file1) {
            String[] t = files.getName().split(".");
            if (files.isFile()) {
                if (files.getName().endsWith(".java")) {
                    System.out.println(files.getName());
                }
            } else if (files.isDirectory()) {
                showDir(files);
            }
        }
    }

    public static void main(String[] args) throws IOException {
//      1、使用大数运算，就算4444444.4444/3.333333的值，保留2位小数，四舍五入"
        BigDecimal a = new BigDecimal("4444444.4444");
        BigDecimal b = new BigDecimal("3.333333");
        BigDecimal result = a.divide(b, 2, RoundingMode.HALF_UP);
        System.out.println("4444444.4444/3.333333的值:" + result);
        System.out.println("=======================");

//      2、输入你生日的前一周的周四是该年的第几天？Calendar:set方法设置到你的生日，add方法跳到前一周的周四，通过 xxxx获得是该年的第几天? get(Calendar.DAY_OF_YEAR)"
        Calendar calendar = Calendar.getInstance();
        calendar.set(2001, 2, 9);
        int week = calendar.get(Calendar.DAY_OF_WEEK) + 2;
        calendar.add(Calendar.DATE, -week);
        System.out.println("是该年的第" + calendar.get(Calendar.DAY_OF_YEAR) + "天");
        System.out.println("=======================");

//      3、递归输出src目录中的所有.java文件的绝对路径
        File file = new File("C:\\Users\\86177\\Desktop\\workspace_idea1\\day4\\src");
        showDir(file);
        System.out.println("=======================");
//      4、在电脑D盘下创建一个文件为HelloWorld.txt文件，判断他是文件还是目录，再创建一个目录IOTest，之后将HelloWorld.txt拷贝到IOTest目录的a.txt去
        File file1 = new File("C:\\Users\\86177\\Desktop\\workspace_idea1\\helloworld.txt");
        if (file1.isDirectory()) {
            System.out.println("是一个目录");

        } else {
            System.out.println("是一个文件");
        }
        File file2 = new File("C:\\Users\\86177\\Desktop\\workspace_idea1\\a.txt");
        createFile(file2);
        Reader r = new FileReader(file1);
        Writer w = new FileWriter(file2);
        char c3[] = new char[4];
        int len;
        while ((len = r.read(c3)) != -1) {
            w.write(c3, 0, len);
        }
        w.close();
        r.close();
        System.out.println("=======================");
//      5、从utf-8编码格式的文本复制到GBK编码的文本（不能出现乱码）
        File file3 = new File("C:\\Users\\86177\\Desktop\\workspace_idea1\\b.txt");
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file1));
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file3), "GBK");
        char bs[] = new char[10];
        while (((len = isr.read(bs)) != -1)) {
            osw.write(new String(bs, 0, len));
        }
        isr.close();
        osw.close();
    }
}